<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>MARC - St. James LifeTeen</title>
	
	<link rel="stylesheet" href="styles/marc.css" />
	<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js" type="text/javascript"></script>

	<script src="js/marcAdmin.js"></script>
	<script src="js/highcharts.js"></script>
	<script src="js/highcharts-more.js"></script>
	<script src="js/modules/exporting.js"></script>
	
	<script type="text/javascript">
		$(document).ready(function()
		{
			//Declare document-scope userid
			var _userid;
			
			//check whether the cookie exists with a userid already stored
			if(getCookie("userlogin") != 'undefined' && getCookie("userlogin") != null)
			{
				//if it does, the userid to pass to AJAX is whatever is in the cookie
				_userid = getCookie("userlogin");
			}
			else
			{
				//otherwise, prompt the user for it
				window.location = "login.html"; //TODO change to gui prompt
			}
			
			//Send the userid to UserInfoService for authentication
			$.ajax({ 
		  	  	url:"/marc/UserInfoService",
				type:'POST',
				data: {
					'method': "CheckForUser",
					'UserId': _userid
				},
				dataType:"json",
				//if it comes back successfully, load the plugin
				success: function(html) 
				{
					$('#globalSettings').data('userData',html["User"]);
					if(html["User"] == null)
						{alert("Username not found!");}
					else
						{loadPlugin();}
				},
				//otherwise, notify the user to of an error
				error: function(html){
					alert("An error occured while gathering user information." +
							"\nPlease refresh your browser and try again." +
							"\n\nIf problem persists please contact BigHelp Ministries");
				}
			});
			
			//loadPlugin() checks the data passed back from UserInfoService to see 
			//which menus the logged-in user has access to
			function loadPlugin()
			{
				var _currUser = $('#globalSettings').data('userData');
				var _userID = _currUser["UserId"];
				
				setCookie("userlogin", _userID, 1);
				$('#loginButton').text("Logged in as " + _userID).attr('title','Click to Log Out');
				
				if(_currUser["Access_Level"] == "Administrator")
				{
					//$('head').append(
					//	$('<script>')
					//		.attr('src','js/marcAdmin.js'));
					$('body').marcAdmin();
				}
				else if(_currUser["Access_Level"] == "Parent")
				{
					$('head').append(
							$('<script>')
								.attr('src','js/marcParent.js'));
					$('body').marcParent();
				}
				else if(_currUser["Access_Level"] == "Student")
				{
					$('head').append(
							$('<script>')
								.attr('src','js/marcStudent.js'));
					$('body').marcStudent();
				}
				else if(_currUser["Access_Level"] == "Inactive")
				{
					$('head').append(
							$('<script>')
								.attr('src','js/marcGuest.js'));
					alert("Hello " + _currUser['Pref_Name'] + "! Your account has been disabled. "
							+ "Please contact your Youth Minister to have your access reinstated.");
					$('body').marcGuest();
				}
				else
				{
					alert("An error has occurred. You do not have access to this system.");
				}
			}
			
			function setCookie(c_name,value,exdays)
			{
				var exdate=new Date();
				exdate.setDate(exdate.getDate() + exdays);
				var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
				document.cookie=c_name + "=" + c_value;
			}
			
			function getCookie(c_name)
			{
				var i,x,y,ARRcookies=document.cookie.split(";");
				for (i=0;i<ARRcookies.length;i++)
				{
				  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
				  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
				  x=x.replace(/^\s+|\s+$/g,"");
				  if (x==c_name)
				    {
				    return unescape(y);
				    }
			  	}
			}
			
			$('#loginButton').click(function(){
				if(getCookie("userlogin") != null && getCookie("userlogin") != '')
				{
					setCookie("userlogin",null,0);
					alert("You have been logged out!");
					location.reload();
				}
				else
				{
					$('#loginButton').text("Login");
					location.reload();
				}
			});
		});
	</script>
</head>

<body>
	<div id="headerDiv">
			<h1>
				MARC - St. James LifeTeen
				<a id="logo" href="./" target="_top"><img src="img/marc_logo.jpg" height="100px" /></a> 
				<button id="loginButton">Login</button>
			</h1>
			<hr />
	</div>
	<div id="appBody"></div>
	<div id="globalSettings" style="display: none;"></div>
</body>
</html>